package com.zenithmind.disk.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.disk.pojo.domain.RecycleBin;
import com.zenithmind.disk.pojo.query.RecycleBinQuery;
import com.zenithmind.disk.pojo.vo.RecycleBinVO;
import com.zenithmind.disk.service.RecycleBinService;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 回收站管理控制器
 * 遵循单一职责原则：仅负责HTTP请求映射和响应处理
 * 遵循依赖倒置原则：依赖抽象接口而非具体实现
 */
@Slf4j
@RestController
@RequestMapping("/api/zenithMind/recycleBin")
@Tag(name = "回收站管理")
@RequiredArgsConstructor
@Validated
public class RecycleBinController {

    private final RecycleBinService recycleBinService;

    @Log(description = "列出回收站文件", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "列出回收站文件")
    @GetMapping("/listRecycleBin")
    public Result<List<RecycleBinVO>> listRecycleBin() {
        log.info("列出回收站文件请求");
        List<RecycleBinVO> recycleBinList = recycleBinService.listRecycleBin();
        return Result.success(recycleBinList);
    }

    @Log(description = "恢复文件", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "恢复文件")
    @PutMapping("/restoreFile")
    public Result<Boolean> restoreFile(
            @RequestParam("fileId") String fileId,
            @RequestParam("userId") String userId) {
        log.info("恢复文件请求：文件ID={}, 用户ID={}", fileId, userId);
        boolean result = recycleBinService.restoreFile(fileId, userId);
        return Result.success(result);
    }

    @Log(description = "彻底删除文件", operationType = OperationTypeEnum.DELETE)
    @Operation(summary = "彻底删除文件")
    @DeleteMapping("/deleteForever")
    public Result<Boolean> deleteForever(
            @RequestParam("fileId") String fileId,
            @RequestParam("userId") String userId) {
        log.info("彻底删除文件请求：文件ID={}, 用户ID={}", fileId, userId);
        boolean result = recycleBinService.deleteForever(fileId, userId);
        return Result.success(result);
    }

    @Log(description = "清空回收站", operationType = OperationTypeEnum.DELETE)
    @Operation(summary = "清空回收站")
    @DeleteMapping("/clearRecycleBin")
    public Result<Boolean> clearRecycleBin() {
        log.info("清空回收站请求");
        boolean result = recycleBinService.clearRecycleBin();
        return Result.success(result);
    }

    @Log(description = "分页查询回收站文件", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "分页查询回收站文件")
    @GetMapping("/recycleBinPage")
    public Result<PageResult<RecycleBinVO>> recycleBinPage(@Validated RecycleBinQuery recycleBinQuery) {
        log.info("分页查询回收站文件请求：{}", recycleBinQuery);
        PageResult<RecycleBinVO> result = recycleBinService.recycleBinPage(recycleBinQuery);
        return Result.success(result);
    }

    @Log(description = "批量删除回收站文件", operationType = OperationTypeEnum.DELETE)
    @Operation(summary = "批量删除回收站文件-管理员")
    @DeleteMapping("/deleteBatchRecycleBin")
    public Result<Boolean> deleteBatchRecycleBin(@Validated @RequestBody List<String> ids) {
        log.info("批量删除回收站文件请求：文件ID列表={}", ids);
        boolean result = recycleBinService.deleteBatchRecycleBin(ids);
        return Result.success(result);
    }

    @Log(description = "添加或修改回收站记录", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "添加或修改回收站记录-管理员")
    @PostMapping("/addOrUpdateRecycleBin")
    public Result<RecycleBinVO> addOrUpdateRecycleBin(@Validated @RequestBody RecycleBin recycleBin) {
        log.info("添加或修改回收站记录请求：{}", recycleBin);
        RecycleBinVO recycleBinVO = recycleBinService.addOrUpdateRecycleBinByAdmin(recycleBin);
        return Result.success(recycleBinVO);
    }
}
